package org.example.myleet.p462;

import java.util.Arrays;

public class Solution {
    public int minMoves2(int[] nums) {
        Arrays.sort(nums);
        int medium = (nums.length & 1) == 1 ? nums[nums.length / 2] : ((nums[nums.length / 2 - 1] + nums[nums.length / 2]) / 2);
        int totalOp = 0;
        for (int num : nums) {
            totalOp += Math.abs(medium - num);
        }
        return totalOp;
    }
}
